/*
Copyright 2011 Raymond Giorgi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package edu.pitt.cs3530;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import edu.pitt.cs3530.xml.ApplicationServerConfig;

public class SystemInfo {

	public enum OperatingSystem {
		UBUNTU, GENTOO, NULL
	};

	private static OperatingSystem sys;

	public static void setOperatingSystem(OperatingSystem os) {
		sys = os;
	}

	public static void setOperatingSystem(String os) {
		if (os.equals(OperatingSystem.UBUNTU.toString())) {
			sys = OperatingSystem.UBUNTU;
		} else if (os.equals(OperatingSystem.GENTOO.toString())) {
			sys = OperatingSystem.GENTOO;
		} else {
			System.out.println(os + ":::" + OperatingSystem.UBUNTU.toString());
		}
	}

	public static String findMacAddress() {
		String mac = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					ApplicationServerConfig.getInstance().getHwaddrLocation()));
			StringTokenizer tok = new StringTokenizer(reader.readLine(), " ");
			for (int i = 0; i < 4; i++) {
				tok.nextToken();
			}
			mac = tok.nextToken();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mac;
	}

	public static String findHostname() {
		String hostname = null;
		try {
			hostname = (new BufferedReader(new FileReader("/etc/hostname")))
					.readLine()
					+ ".cs.pitt.edu";
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hostname;
	}

	public static void executeCommand(String command) throws IOException,
			InterruptedException {
		if (sys == OperatingSystem.UBUNTU) {
			command = "sudo " + command;
		}
		System.out.println(command);
		Process pr = Runtime.getRuntime().exec(command);
		pr.waitFor();
	}
}
